/**
 *品质365 公用组件
 *date 2012/11/15
 */
var load = false;
$(function(){
	/*
	* 智能机浏览器版本信息:
	*
	*/
	  var browser={
	    versions:function(){ 
	           var u = navigator.userAgent, app = navigator.appVersion; 
	           return {//移动终端浏览器版本信息 
	                mobile: !!navigator.userAgent.match(/AppleWebKit.*Mobile./), //是否为移动终端
	                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
	                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1 //android终端或者uc浏览器
	            };
	         }(),
	         language:(navigator.browserLanguage || navigator.language).toLowerCase()
	} 

	if (browser.versions.mobile) {

		var fixDiv="";

		if (browser.versions.ios) {
		     fixDiv = "<a href='https://itunes.apple.com/us/app/pin-zhi365/id680435375?mt=8' class='mobile_fix'><img src='/irolmex/images/mobile/download_button.png'></a>";
		};

		if (browser.versions.android) {
			 fixDiv = "<a href='http://www.pinzhi365.com/FileDownServlet?filename=pinzhi-v01.apk' class='mobile_fix'><img src='/irolmex/images/mobile/download_button.png'></a>";
		};

		$("body").append(fixDiv);
		
	}

	
	if(!load){
		$.ajax({
			url:'/header.do',
			type:'POST',
			dataType:'html',
			async: false,
			success:function(data){
				$('#app-header').html(data);	
			if($('#app-header').get(0)){
					loadLoginUserStatus();
				};	
				
			}
		});
		$.ajax({
			url:'/header_min.do',
			type:'POST',
			dataType:'html',
			async: false,
			success:function(data){
				$('#app-header-min').html(data);
				if($('#app-header-min').get(0)){
					loadLoginUserStatus();
				};
			}
		});
		$.ajax({
			url:'/header_min_cart.do',
			type:'POST',
			dataType:'html',
			async: false,
			success:function(data){
				$('#app-header-min-cart').html(data);
				if($('#app-header-min-cart').get(0)){
					loadLoginUserStatus();
				};
			}
		});
		$.ajax({
			url:'/footer.do',
			type:'POST',
			dataType:'html',
			async: false,
			success:function(data){
				$('#app-footer').html(data);
				var ga = "var _gaq=_gaq||[];_gaq.push(['_setAccount','UA-37135219-1']),_gaq.push(['_trackPageview']),function(){var e=document.createElement('script');e.type='text/javascript',e.async=!0,e.src=('https:'==document.location.protocol?'https://ssl':'http://www')+'.google-analytics.com/ga.js';var t=document.getElementsByTagName('script')[0];t.parentNode.insertBefore(e,t)}();";
				eval(ga);
				// 帮助中心
				$.ajax({
					url:'/loadFootHelpers.do',
					type:'POST',
					dataType:'json',
					async: false,
					success:function(data){
						creadHelpData(data);
					}
				});
			}
		});
		$.ajax({
			url:'/footer_min.do',
			type:'POST',
			dataType:'html',
			async: false,
			success:function(data){
				$('#app-footer-min').html(data);
			}
		});
		
		
		
		
		
		//搜索模块
		$('.searchText').live("click",function(event) {
		    $(this).parent().addClass("ph-focus").removeClass("ph-hover");
		})
		.live("blur",function(event) {
		    var value = $(this).val();
		    if (value=="") {
		      $('.ph-label').show();
		    }
		    $(this).parent().removeClass("ph-focus").addClass("ph-hover");
		    $('.mall-suggest-container').hide();
		});
		
		load = true;
	}
	
	
	
	
	//分享
	
			var shareList = "<div id='pzShare' class='share_content' style='display:none'>"+
		    " <h6><a href='javascript:' class='s_close'>关闭</a>立即分享<span>（分享成功后可以获得名望值哦~）</span></h6>"+
		     "<ul>"+
		     "  <li id='sinawb'><a href='javascript:' class='bds_tsina xlwb'>新浪微博</a></li>"+
		     "  <li id='qzone'><a href='javascript:' class='bds_qzone qqkj'>QQ空间</a></li>"+
		     " <li id='renren'><a href='javascript:' class='bds_renren rrw'>人人网</a></li>"+
		     " <li id='qqwb'><a href='javascript:' class='bds_tqq txwb'>腾讯微博</a></li>"+
		     "  <li id='kaixin'><a href='javascript:' class='bds_kaixin001 kxw'>开心网</a></li>"+
		    " </ul>"+
			"</div>";
		
		
			var shareTypeId = $("#shareTypeId").val();
			var productId = $("#productId").val();
			var agentId = $("#agentId").val();
			$('body').append(shareList);
		
		
			$("#shareBtn").live("click",function(){
		
				$("#pzShare").fadeIn().center();
				appendThick();
		
		
				$.getJSON("/shareManager.do?callback=?&action=getShareInfo",{"shareTypeId":shareTypeId,"productId":productId,"agentId":agentId},function(data){
		
						var  title = data.message;
		
						var  url =   encodeURIComponent(data.link);
		
						var  content = data.message;
						
						var  img = data.imgUrl;
						
				MAP = {
		   				sinawb : 'http://v.t.sina.com.cn/share/share.php?appkey=3421054943&title=' + content + '&url=' + url + '&source=bshare&retcode=0&pic='+img,
		   				qqwb : 'http://v.t.qq.com/share/share.php?title=' + content + '&site=' + url + '&pic='+img+'&url='+ url + '&appkey=801435572',
		   				renren : 'http://widget.renren.com/dialog/share?resourceUrl=' + url + '&title=' + '&images=&description=' + content+'image_src='+img,
		   				qzone : 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + url + '&title=' + '&pics='+img+'&summary=' + content,
		   				kaixin : 'http://www.kaixin001.com/rest/records.php?url='+url +'&content=' + content+'&pic='+img+'&aid=100013770&style=111'
		   			};
		
				});
			});
		
			$(".s_close").live("click",function(){
		
				$("#pzShare").hide();
				removeThick();
		
			});
			$("#pzShare").find('li').click(function(){
		   	var url = MAP[$(this).attr('id')],
		   			top = $(window).height() > 400?($(window).height() - 400)/2 : 50,
		   			left = $(window).width() > 600?($(window).width() - 600)/2 : 50;
		   			window.open(url,'', 'height=400, width=600,left='+left+',top=' + top);
		
		   	var snsId =$(this).attr('id');
		   	$.getJSON("/shareManager.do?callback=?&action=getFameFromShare",{"cps":snsId,"objType":shareTypeId,"objId":productId},function(data){
		   			//console.log(data);
		   			if (data.result=="1") {
		   				//alert("分享成功！！！");
		   			};
		
		   	});
		   });
	//分享end
});

//加载用户登录状态
function loadLoginUserStatus(){
	var url = "/loadLoginUserStatus.do?dumy=" + Math.random()+"&callback=?";
	$.getJSON(url,function(json){
		if(json.loginId != '' && json.loginId != 'null' && json.loginId != null){
			var loginState = "<a href='/userHome.do?action=homeIndex'>我的账户</a><span>|</span><a href='/userLoginOut.do'>退出</a><span id='userInfo_span'>您好&nbsp;&nbsp;"+json.loginId+"&nbsp;欢迎光临品质365商城！</span>";
			$('#userLoginState').html(loginState);
		}else{
			var loginState = "<a href='/userHome.do?action=homeIndex'>我的账户</a><span>|</span><a id='userReg_href' href='/userReg.do?action=register'>免费注册</a><span>|</span><a id='goToLoginForm' href='/userLogin.do?action=loginForm'>请登录</a><span id='userInfo_span'>您好，欢迎光临品质365商城！</span>";
			$('#userLoginState').html(loginState);
		}
	});
	loadMiniShoppingCartInfo();
}
//加载页头小购物车信息
function loadMiniShoppingCartInfo(){
	var url = "/loadMiniShoppingCart.do?callback=?&dumy="+Math.random();
	$.getJSON(url,function(json){
		var productTotalCount = json.shoppingCartInfo.productTotalCount;
		var miniShoppingInfoHtml = "购物车("+productTotalCount+")";
		$('#miniShoppingCart').html(miniShoppingInfoHtml);
		$('#miniShoppingCart').show();
	},"json");
}

//帮助中心
var creadHelpData = function(data){
  var helpData = data.data,
      aboutData = data.footLinkData,
      helpDataNum = helpData.length,
      helpDtHtml = "",
      helpDataHtml = "";
  for(i=0;i< helpDataNum;i++){
      helpDtHtml="<span>"+helpData[i].title+"</span>";
      var helpDataChildData = helpData[i].children,
          helpDataChildNum = helpDataChildData.length,
          helpDataChildDd = "";
      for(j=0;j< helpDataChildNum;j++){

          helpDataChildDd+="<span><a href='"+helpData[i].children[j].link+"'>"+helpData[i].children[j].title+"</a></span>"
      }
       
       helpDataHtml+=("<dl><dt class='help-bg-"+i+"'>"+helpDtHtml+"</dt><dd>"+helpDataChildDd+"</dd></dl>");               
   }

  $('.help-desc').html(helpDataHtml+"<div class='tel'><img src='/irolmex/images/tel.png'></div>");
  //关于我们
  creadAbout(aboutData);
}

// 关于我们
var creadAbout = function(data){
  var aboutNum = data.length,
      aboutHtml = ""
  for(i=0;i< aboutNum;i++){
	  if(data[i].title=='友情链接'){
		  aboutHtml+="<a href='/links/listLinks.do' target='_blank'>"+data[i].title+"</a><span>|</span>"
	  }else{
		  aboutHtml+="<a href='"+data[i].link+"' target='_blank'>"+data[i].title+"</a><span>|</span>"
	  }
  }
  var spanRemove =aboutNum - 1;
  $("#footer-info").html(aboutHtml).find("span:eq("+spanRemove+")").remove()
}  

function checkTopSearchForm(){
    var keyword=jQuery("#topKeywords").val();
    var keywords = "";
    if(keyword!=""){
    	keywords = keyword+",title,keyword";
    }
    keywords = encodeURI(jQuery.trim(keywords));
    keyword = encodeURI(jQuery.trim(keyword));
    jQuery("#thKeywords").val(keywords);
    jQuery("#keywords").val(keyword);
 }

function doTopSearch(){
	checkTopSearchForm();
	document.getElementById("topSearchForm").submit();
}


function loadQRImage(url){
	this.url=url;
	var _this=this;
	//加载图片
	this.preLoadImg(this.url,function(){
		_this.insertImage();
	});
};
loadQRImage.prototype={
	preLoadImg:function(url,callBack){
			var img=new Image();
			if(!!window.ActiveXObject){
					img.onreadystatechange=function(){
						if(this.readyState=="complete"){
									callBack();
								};
						};
				}else{
					img.onload=function(){
							callBack();
						};
					};
			img.src=url;
	},
	insertImage:function(){
		//javascript:window.open("http://b.qq.com/webc.htm?new=0&sid=4000106365&o=http://www.pinzhi365.com/&q=7","_blank","height=502, width=644,toolbar=no,scrollbars=no,menubar=no,status=no");
		//<img style='z-index:1;' src='"+this.url+"' />
		$("<div style='background:url("+this.url+") no-repeat center center;'><a href='http://wpa.qq.com/msgrd?v=3&uin=4008988365&site=qq&menu=yes' target='_blank'  style='position:absolute; z-index:2;top:0px;left:0px;display:block;width:66px;height:87px;'></a><a id='shareBtn' href='javascript:' style=' position:absolute;bottom:0px;right:0px;display:block;width:66px;height:87px;' ></a></div>").css({
											"width":66,
											"height":181,
											"position":"fixed",
											"_position":"absolute",
											"bottom":"50px",
											"left":"50%",
											"marginLeft":"550px",
											"display":"none"
											}).appendTo("body").fadeIn(2000);
	}
	
};
//创建一个二维码图片
var QR=new loadQRImage("/irolmex/images/dc.png");





/**
*品质365
*date 2012/11/06
*底层通用函数
*/
(function($){
//当前位置居中
$.fn.center = function(){
var top = ($(window).height() - this.height())/2;
var left = ($(window).width() - this.width())/2;
var scrollTop = $(document).scrollTop();
var scrollLeft = $(document).scrollLeft();
if (top<0) {
var top = 30;
}
return this.css( { position : 'absolute', 'top' : top+scrollTop, left : left + scrollLeft } );
}
})(jQuery)
//tab切换
function tabChange(dom,obj,tabOn,tabShow){
obj.addClass(tabOn).siblings().removeClass(tabOn);
obj.attr("checked","checked").siblings().removeAttr("checked");
var index = dom.index(obj);
$(tabShow).eq(index).show().siblings().hide();
}
//插入蒙层
function appendThick(){
var docu = $('body');
var hei= docu.height();
docu.append("<div class='thickdiv'></div>");
$('.thickdiv').css("height",hei);
}
//蒙层remove
function removeThick(){
$('.thickdiv').remove();
}
//提示层显示
function tripLayer(){
$('.bodyTrip').show().center();
var hei = $('.layer-box-trip').height();
$('.layer-skin').height(hei +(20));
}
//提示层隐藏
function hideTripLayer(){
$('.bodyTrip').hide();
}
/**
* 隐藏窗体
* @param obj
*/
function hideDialog(obj){
$(obj).hide();
}


